# encoding: utf-8
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from exts import db
from BBS import create_app
from apps.cms.models import CMSUser, CMSPermission, CMSRole
from apps.front.models import FrontUser
from apps.models import BannerModel,BoardModel,PostModel

app = create_app()
manager = Manager(app)
Migrate(app, db)
manager.add_command('db', MigrateCommand)


@manager.option('-u', '--username', dest='username')
@manager.option('-p', '--password', dest='password')
@manager.option('-e', '--email', dest='email')
def create_cmsUser(username, password, email):
    user = CMSUser(username=username, password=password, email=email)
    db.session.add(user)
    db.session.commit()
    print('添加CMS用户成功！')


@manager.command
def create_role():
    # 1.访问者（只能浏览，修改个人信息）
    visitor = CMSRole(name='访问者', desc='只能浏览信息，及修改个人信息！')
    visitor.permissions = CMSPermission.VISITOR

    # 2.运营者（除拥有访问者的权限外，还管理帖子，评论，板块，及前台用户）
    operator = CMSRole(name='运营者', desc='除拥有访问者的权限外，还管理帖子，评论，及前台用户')
    operator.permissions = CMSPermission.VISITOR | CMSPermission.POSTER | CMSPermission.COMMENTER | CMSPermission.FRONTUSER | CMSPermission.CMSUSER

    # 3.管理员（除运营者的权限外，还拥有后台用户的管理权限）
    admin = CMSRole(name='管理员', desc='拥有所有权限')
    admin.permissions = CMSPermission.VISITOR | CMSPermission.POSTER | CMSPermission.COMMENTER | CMSPermission.FRONTUSER | CMSPermission.BOARDER | CMSPermission.CMSUSER

    # 4.开发者权限（拥有绝对的权限）
    developer = CMSRole(name='开发者', desc='拥有系统的绝对权限')
    developer.permissions = CMSPermission.ALL_PERMISSION

    db.session.add_all([visitor, operator, admin, developer])
    db.session.commit()
    print('权限添加成功')


@manager.command
def test_permission():
    user = CMSUser.query.first()
    if user.is_developer:
        print('这个用户拥有开发者的权限！')
    else:
        print('这个用户没有开发者的权限！')


@manager.option('-e', '--email', dest='email')
@manager.option('-n', '--name', dest='name')
def add_user2role(email, name):
    user = CMSUser.query.filter_by(email=email).first()
    if user:
        role = CMSRole.query.filter_by(name=name).first()
        if role:
            role.users.append(user)
            db.session.commit()
            print('用户成功添加到角色中！')
        else:
            print('【%s】 角色不存在！' % name)
    else:
        print('【%s】 邮箱不存在！' % email)


####front###
@manager.option('-t', '--telephone', dest='telephone')
@manager.option('-u', '--username', dest='username')
@manager.option('-p', '--password', dest='password')
def create_fuser(telephone, username, password):
    user = FrontUser(telephone=telephone, username=username, password=password)
    db.session.add(user)
    db.session.commit()
    print('front用户添加成功！')


@manager.command
def create_post():
    for i in range(206):
        title = '%s' %i
        content = '%s' %i
        board = BoardModel.query.first()
        author = FrontUser.query.first()
        post = PostModel(title=title,content=content)
        post.board = board
        post.author = author
        db.session.add(post)
        db.session.commit()
        print('帖子添加成功！')


if __name__ == '__main__':
    manager.run()
